class Video {
  final String id;
  final String title;
  final String description;
  final String thumbnailUrl;
  final String videoUrl;
  final String? offlineUrl; 
  final String category;
  final int year;
  final String duration;
  final double rating;
  final List<String> tags;
  final List<String> cast;
  final String director;
  final bool isOriginal;
  final DateTime releaseDate;
  final int viewCount;
  final int likeCount;
  final int commentCount;
  final bool isFavorite;
  final int watchProgress; 
  final int? fileSize; 

  Video({
    required this.id,
    required this.title,
    required this.description,
    required this.thumbnailUrl,
    required this.videoUrl,
    this.offlineUrl, 
    required this.category,
    required this.year,
    required this.duration,
    this.rating = 0.0,
    this.tags = const [],
    this.cast = const [],
    this.director = '',
    this.isOriginal = false,
    required this.releaseDate,
    this.viewCount = 0,
    this.likeCount = 0,
    this.commentCount = 0,
    this.isFavorite = false,
    this.watchProgress = 0,
    this.fileSize,
  });

  factory Video.fromJson(Map<String, dynamic> json) {
    return Video(
      id: json['id'],
      title: json['title'],
      description: json['description'],
      thumbnailUrl: json['thumbnail_url'],
      videoUrl: json['video_url'],
      offlineUrl: json['offline_url'], 
      category: json['category'],
      year: json['year'],
      duration: json['duration'],
      rating: json['rating']?.toDouble() ?? 0.0,
      tags: List<String>.from(json['tags'] ?? []),
      cast: List<String>.from(json['cast'] ?? []),
      director: json['director'] ?? '',
      isOriginal: json['is_original'] ?? false,
      releaseDate: DateTime.parse(json['release_date']),
      viewCount: json['view_count'] ?? 0,
      likeCount: json['like_count'] ?? 0,
      commentCount: json['comment_count'] ?? 0,
      isFavorite: json['is_favorite'] ?? false,
      watchProgress: json['watch_progress'] ?? 0,
      fileSize: json['file_size'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'thumbnail_url': thumbnailUrl,
      'video_url': videoUrl,
      'offline_url': offlineUrl, 
      'category': category,
      'year': year,
      'duration': duration,
      'rating': rating,
      'tags': tags,
      'cast': cast,
      'director': director,
      'is_original': isOriginal,
      'release_date': releaseDate.toIso8601String(),
      'view_count': viewCount,
      'like_count': likeCount,
      'comment_count': commentCount,
      'is_favorite': isFavorite,
      'watch_progress': watchProgress,
      'file_size': fileSize,
    };
  }

  Video copyWith({
    String? id,
    String? title,
    String? description,
    String? thumbnailUrl,
    String? videoUrl,
    String? offlineUrl,
    String? category,
    int? year,
    String? duration,
    double? rating,
    List<String>? tags,
    List<String>? cast,
    String? director,
    bool? isOriginal,
    DateTime? releaseDate,
    int? viewCount,
    int? likeCount,
    int? commentCount,
    bool? isFavorite,
    int? watchProgress,
    int? fileSize,
  }) {
    return Video(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
      thumbnailUrl: thumbnailUrl ?? this.thumbnailUrl,
      videoUrl: videoUrl ?? this.videoUrl,
      offlineUrl: offlineUrl ?? this.offlineUrl,
      category: category ?? this.category,
      year: year ?? this.year,
      duration: duration ?? this.duration,
      rating: rating ?? this.rating,
      tags: tags ?? this.tags,
      cast: cast ?? this.cast,
      director: director ?? this.director,
      isOriginal: isOriginal ?? this.isOriginal,
      releaseDate: releaseDate ?? this.releaseDate,
      viewCount: viewCount ?? this.viewCount,
      likeCount: likeCount ?? this.likeCount,
      commentCount: commentCount ?? this.commentCount,
      isFavorite: isFavorite ?? this.isFavorite,
      watchProgress: watchProgress ?? this.watchProgress,
      fileSize: fileSize ?? this.fileSize,
    );
  }
}
